package com.openspring.business.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.openspring.framework.common.Paging;
import com.openspring.framework.controller.GenericController;
import com.openspring.framework.tree.Node;
import com.openspring.framework.util.FileUpload;
import com.openspring.framework.util.ResultUtils;
import com.openspring.business.model.Employee;
import com.openspring.business.service.EmployeeService;

@Controller
@RequestMapping("employee")
public class EmployeeController extends GenericController<Employee, Long>{

	private EmployeeService employeeService;
	
	@Resource(name="employeeService")
	public void setEmployeeService(EmployeeService employeeService) {
		super.setGenericService(employeeService);
		this.employeeService = employeeService;
	}
	
	@Override
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}
	
	@RequestMapping(value="/create", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> create(@ModelAttribute Employee entity, @RequestParam("file") MultipartFile file) {
		if(!file.isEmpty()) {
			entity.setPhoto(FileUpload.writeImage(file));
		}
		employeeService.create(entity);
		return ResultUtils.data(entity);
	}

	@RequestMapping(value="/modify", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modify(@ModelAttribute Employee entity, @RequestParam("file") MultipartFile file) {
		if(!file.isEmpty()) {
			entity.setPhoto(FileUpload.writeImage(file));
		}
		employeeService.modify(entity);
		return ResultUtils.data(entity);
	}
	
	@RequestMapping(value="/remove", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> remove(@RequestParam("id") Long[] id) {
		if(id.length == 1)
			employeeService.remove(id[0]);
		else
			employeeService.remove(id);
		return ResultUtils.success();
	}
	
	@RequestMapping(value="/load/{id}", method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> load(@PathVariable("id") Long id) {
		return ResultUtils.data(employeeService.load(id));
	}
	
	@RequestMapping(value="/list", method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> list(
			@ModelAttribute Paging paging,
			@RequestParam(value="filter", required=false) String filter,
			@RequestParam(value="sort", required=false) String sort) {
		
		return this.doQuery(paging, filter, sort);
	}
	
	@RequestMapping(value="/tree", method=RequestMethod.GET)
	@ResponseBody
	public Node tree() {
		return employeeService.getEmployeeTree();
	}
}